package se.webbzon.boltzmann.game.object.waypoint;

import java.awt.Color;
import java.util.ArrayList;

import javax.vecmath.Point3d;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.oschi01.path3d.Path3D;
import se.webbzon.oschi01.worldeditor.DeflatableWorldObject;
import se.webbzon.oschi01.worldeditor.WorldObjectDeflation;
import se.webbzon.oschi01.worldobject.WorldStaticObject;
import se.webbzon.oschi01.worldobject.appearance.Appearance3D;
import se.webbzon.oschi01.worldobject.appearance.EllipsoidAppearance3D;
import se.webbzon.oschi01.worldobject.appearance.NoAppearance3D;

public class Waypoint extends WorldStaticObject implements DeflatableWorldObject {

	// The name of this deflatable world object
	public static final String worldObjectName = "waypoint";
	
	// The next waypoint
	private Waypoint next;
	
	/*============================================================
	Constructors
	============================================================*/
	
	/** Creates a new waypoint. **/
	public Waypoint(AbstractEnvironment env) {
		super(0,createAppearance(env));
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Creates a new 3-dimensional path starting at this waypoint.
	 * The path will be generated iteratively by calling the getNext()
	 * method of the connected waypoints. **/
	public Path3D makePath3D() {
		ArrayList<Point3d> points = new ArrayList<Point3d>();
		points.add(getPosition());
		Waypoint waypoint = this;
		while (waypoint.getNext() != null) {
			waypoint = waypoint.getNext();
			points.add(waypoint.getPosition());
		}
		return new Path3D(points);
	}
	
	/** Returns the next waypoint. **/
	public Waypoint getNext() {
		return next;
	}
	
	/** Sets the next waypoint. **/
	public void setNext(Waypoint next) {
		this.next = next;
	}
	
	@Override public String getName() {
		return worldObjectName;
	}

	@Override public WorldObjectDeflation getDeflation() {
		return new WaypointDeflation(0,this,next);
	}
	
	/*============================================================
	Private Methods
	============================================================*/
	
	/** Creates the appearance of this waypoint. **/
	private static final Appearance3D createAppearance(AbstractEnvironment env) {
		if (env.isDebugMode()) {
			EllipsoidAppearance3D appearance = new EllipsoidAppearance3D();
			appearance.setColor(Color.blue);
			return appearance;
		} else
			return new NoAppearance3D();
	}

}
